package com.qingcloud.adminbackend.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.base.AjaxResult;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

/**
 * @Desc JWT工具类
 **/
public class JwtUtil {
    public static void responseError(ServletResponse response, Integer code, String errorMsg) {
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		AjaxResult jsonResult = new AjaxResult();
		jsonResult.setErroCode(code);
		jsonResult.setErroMsg(errorMsg);
        OutputStream os = null;
        try {
            os = httpServletResponse.getOutputStream();
			httpServletResponse.setCharacterEncoding("UTF-8");
			httpServletResponse.setStatus(401);
            os.write(new ObjectMapper().writeValueAsString(jsonResult).getBytes("UTF-8"));
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	/**
	 * 校验token是否正确
	 *
	 * @param token  密钥
	 * @param secret 用户的密码
	 * @return 是否正确
	 */
	public static boolean verify(String token, String username, String secret) {
		try {
			// 根据密码生成JWT效验器
			Algorithm algorithm = Algorithm.HMAC256(secret);
			JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build();
			// 效验TOKEN
			DecodedJWT jwt = verifier.verify(token);
			return true;
		} catch (Exception exception) {
			return false;
		}
	}

	/**
	 * 获得token中的信息无需secret解密也能获得
	 *
	 * @return token中包含的用户名
	 */
	public static String getUsername(String token) {
		try {
			DecodedJWT jwt = JWT.decode(token);
			return jwt.getClaim("username").asString();
		} catch (JWTDecodeException e) {
			return null;
		}
	}

	/**
	 * 生成签名,5min后过期
	 *
	 * @param username 用户名
	 * @param secret   用户的密码
	 * @return 加密的token
	 */
	public static String sign(String username, String secret) {
		Date date = new Date(System.currentTimeMillis() + WamingConstant.TOKEN_EXPIRE_TIME*1000);
		Algorithm algorithm = Algorithm.HMAC256(secret);
		// 附带username信息
		return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm);

	}

	/**
	 * 根据request中的token获取用户账号
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public static String getUserNameByToken(HttpServletRequest request) throws Exception {
		String accessToken = request.getHeader("X-Access-Token");
		String username = getUsername(accessToken);
		if (StringUtils.isEmpty(username)) {
			throw new Exception("未获取到用户");
		}
		return username;
	}

//	/**
//	  *  从session中获取变量
//	 * @param key
//	 * @return
//	 */
//	public static String getSessionData(String key) {
//		//${myVar}%
//		//得到${} 后面的值
//		String moshi = "";
//		if(key.indexOf("}")!=-1){
//			 moshi = key.substring(key.indexOf("}")+1);
//		}
//		String returnValue = null;
//		if (key.contains("#{")) {
//			key = key.substring(2,key.indexOf("}"));
//		}
//		if (StringUtils.isNotEmpty(key)) {
//			HttpSession session = SpringContextUtils.getHttpServletRequest().getSession();
//			returnValue = (String) session.getAttribute(key);
//		}
//		//结果加上${} 后面的值
//		if(returnValue!=null){returnValue = returnValue + moshi;}
//		return returnValue;
//	}
}
